{
 "nbformat": 4,
 "nbformat_minor": 0,
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# RASA x LanceDB x LLM : Conversational chatbot"
   ],
   "metadata": {
    "id": "5AUJAGW2Rqmm"
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Rasa is an open-source framework for building intelligent, contextual, and scalable conversational agents. It provides robust tools for **natural language understanding (NLU)**,**dialogue management** and **custom actions**, enabling the creation of sophisticated chatbots and virtual assistants tailored to specific business needs. With its flexible architecture, Rasa allows seamless integration with various APIs, databases, and machine learning models, facilitating the development of highly responsive and intelligent conversational experiences.\n",
    "\n",
    "Explore RASA at : https://rasa.com/docs/rasa/\n",
    "\n",
    "Explore LanceDB at : https://lancedb.github.io/lancedb/\n",
    "\n",
    "![](https://media4.giphy.com/media/v1.Y2lkPTc5MGI3NjExenZjZDhsbnp2cGxibW1zbmQ1Ymc2ZTgzbzBzbDRwZ2t3MzZsd3hvaCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/S0hxMGYFhEMzm/giphy.webp)\n",
    "\n",
    "Today we will be building an **Advanced Customer Support Chatbot** using Rasa, LanceDB, and OpenAI api to deliver a great customer service experience. This chatbot is designed to handle a wide range of customer inquiries, provide accurate information, and offer personalized assistance, all while maintaining a natural and engaging conversational flow.\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ],
   "metadata": {
    "id": "vEkX0bDnPU15"
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "3Bz7xn6a6OdX"
   },
   "outputs": [],
   "source": [
    "# Install required packages\n",
    "!pip install rasa lancedb openai==0.28 python-dotenv -q"
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "# Initialize a new Rasa project which sets up the necessary directory structure and files.\n",
    "!rasa init --no-prompt"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "9fpWSlpm6iA4",
    "outputId": "8c342841-5f2b-423e-8267-4a5ae8d241d1"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "/usr/local/lib/python3.10/dist-packages/rasa/core/tracker_store.py:1044: MovedIn20Warning: \u001b[31mDeprecated API features detected! These feature(s) are not compatible with SQLAlchemy 2.0. \u001b[32mTo prevent incompatible upgrades prior to updating applications, ensure requirements files are pinned to \"sqlalchemy<2.0\". \u001b[36mSet environment variable SQLALCHEMY_WARN_20=1 to show all deprecation warnings.  Set environment variable SQLALCHEMY_SILENCE_UBER_WARNING=1 to silence this message.\u001b[0m (Background on SQLAlchemy 2.0 at: https://sqlalche.me/e/b8d9)\n",
      "  Base: DeclarativeMeta = declarative_base()\n",
      "\u001b(0lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk\u001b(B\n",
      "\u001b(0x\u001b(B Rasa Open Source reports anonymous usage telemetry to help improve the product \u001b(0x\u001b(B\n",
      "\u001b(0x\u001b(B for all its users.                                                             \u001b(0x\u001b(B\n",
      "\u001b(0x\u001b(B                                                                                \u001b(0x\u001b(B\n",
      "\u001b(0x\u001b(B If you'd like to opt-out, you can use `rasa telemetry disable`.                \u001b(0x\u001b(B\n",
      "\u001b(0x\u001b(B To learn more, check out https://rasa.com/docs/rasa/telemetry/telemetry.       \u001b(0x\u001b(B\n",
      "\u001b(0mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj\u001b(B\n",
      "/usr/local/lib/python3.10/dist-packages/rasa/shared/utils/validation.py:134: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html\n",
      "  import pkg_resources\n",
      "/usr/local/lib/python3.10/dist-packages/pkg_resources/__init__.py:3154: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('google')`.\n",
      "Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages\n",
      "  declare_namespace(pkg)\n",
      "/usr/local/lib/python3.10/dist-packages/pkg_resources/__init__.py:3154: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('google.cloud')`.\n",
      "Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages\n",
      "  declare_namespace(pkg)\n",
      "/usr/local/lib/python3.10/dist-packages/pkg_resources/__init__.py:3154: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('mpl_toolkits')`.\n",
      "Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages\n",
      "  declare_namespace(pkg)\n",
      "/usr/local/lib/python3.10/dist-packages/pkg_resources/__init__.py:3154: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('ruamel')`.\n",
      "Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages\n",
      "  declare_namespace(pkg)\n",
      "/usr/local/lib/python3.10/dist-packages/pkg_resources/__init__.py:3154: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('sphinxcontrib')`.\n",
      "Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages\n",
      "  declare_namespace(pkg)\n",
      "\u001b[92mWelcome to Rasa! 🤖\n",
      "\u001b[0m\n",
      "To get started quickly, an initial project will be created.\n",
      "If you need some help, check out the documentation at https://rasa.com/docs/rasa.\n",
      "\n",
      "2024-12-30 20:25:43 \u001b[1;30mINFO    \u001b[0m \u001b[34mroot\u001b[0m  - creating tests\n",
      "2024-12-30 20:25:43 \u001b[1;30mINFO    \u001b[0m \u001b[34mroot\u001b[0m  - copying /usr/local/lib/python3.10/dist-packages/rasa/cli/initial_project/tests/test_stories.yml -> ./tests\n",
      "2024-12-30 20:25:43 \u001b[1;30mINFO    \u001b[0m \u001b[34mroot\u001b[0m  - copying /usr/local/lib/python3.10/dist-packages/rasa/cli/initial_project/config.yml -> .\n",
      "2024-12-30 20:25:43 \u001b[1;30mINFO    \u001b[0m \u001b[34mroot\u001b[0m  - copying /usr/local/lib/python3.10/dist-packages/rasa/cli/initial_project/endpoints.yml -> .\n",
      "2024-12-30 20:25:43 \u001b[1;30mINFO    \u001b[0m \u001b[34mroot\u001b[0m  - copying /usr/local/lib/python3.10/dist-packages/rasa/cli/initial_project/credentials.yml -> .\n",
      "2024-12-30 20:25:43 \u001b[1;30mINFO    \u001b[0m \u001b[34mroot\u001b[0m  - copying /usr/local/lib/python3.10/dist-packages/rasa/cli/initial_project/domain.yml -> .\n",
      "2024-12-30 20:25:43 \u001b[1;30mINFO    \u001b[0m \u001b[34mroot\u001b[0m  - creating data\n",
      "2024-12-30 20:25:43 \u001b[1;30mINFO    \u001b[0m \u001b[34mroot\u001b[0m  - copying /usr/local/lib/python3.10/dist-packages/rasa/cli/initial_project/data/stories.yml -> ./data\n",
      "2024-12-30 20:25:43 \u001b[1;30mINFO    \u001b[0m \u001b[34mroot\u001b[0m  - copying /usr/local/lib/python3.10/dist-packages/rasa/cli/initial_project/data/nlu.yml -> ./data\n",
      "2024-12-30 20:25:43 \u001b[1;30mINFO    \u001b[0m \u001b[34mroot\u001b[0m  - copying /usr/local/lib/python3.10/dist-packages/rasa/cli/initial_project/data/rules.yml -> ./data\n",
      "2024-12-30 20:25:43 \u001b[1;30mINFO    \u001b[0m \u001b[34mroot\u001b[0m  - creating actions\n",
      "2024-12-30 20:25:43 \u001b[1;30mINFO    \u001b[0m \u001b[34mroot\u001b[0m  - copying /usr/local/lib/python3.10/dist-packages/rasa/cli/initial_project/actions/__init__.py -> ./actions\n",
      "2024-12-30 20:25:43 \u001b[1;30mINFO    \u001b[0m \u001b[34mroot\u001b[0m  - copying /usr/local/lib/python3.10/dist-packages/rasa/cli/initial_project/actions/actions.py -> ./actions\n",
      "2024-12-30 20:25:43 \u001b[1;30mINFO    \u001b[0m \u001b[34mroot\u001b[0m  - creating actions/__pycache__\n",
      "2024-12-30 20:25:43 \u001b[1;30mINFO    \u001b[0m \u001b[34mroot\u001b[0m  - copying /usr/local/lib/python3.10/dist-packages/rasa/cli/initial_project/actions/__pycache__/__init__.cpython-310.pyc -> ./actions/__pycache__\n",
      "2024-12-30 20:25:43 \u001b[1;30mINFO    \u001b[0m \u001b[34mroot\u001b[0m  - copying /usr/local/lib/python3.10/dist-packages/rasa/cli/initial_project/actions/__pycache__/actions.cpython-310.pyc -> ./actions/__pycache__\n",
      "Created project directory at '/content'.\n",
      "\u001b[92mFinished creating project structure.\u001b[0m\n",
      "\u001b[92mTraining an initial model...\u001b[0m\n",
      "/usr/local/lib/python3.10/dist-packages/tensorflow/lite/python/util.py:52: DeprecationWarning: jax.xla_computation is deprecated. Please use the AOT APIs.\n",
      "  from jax import xla_computation as _xla_computation\n",
      "2024-12-30 20:25:49 \u001b[1;30mINFO    \u001b[0m \u001b[34mnumexpr.utils\u001b[0m  - NumExpr defaulting to 2 threads.\n",
      "\u001b[94mThe configuration for policies and pipeline was chosen automatically. It was written into the config file at 'config.yml'.\u001b[0m\n",
      "2024-12-30 20:25:53 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Starting to train component 'RegexFeaturizer'.\n",
      "2024-12-30 20:25:53 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Finished training component 'RegexFeaturizer'.\n",
      "2024-12-30 20:25:53 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Starting to train component 'LexicalSyntacticFeaturizer'.\n",
      "2024-12-30 20:25:53 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Finished training component 'LexicalSyntacticFeaturizer'.\n",
      "2024-12-30 20:25:53 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Starting to train component 'CountVectorsFeaturizer'.\n",
      "2024-12-30 20:25:53 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.nlu.featurizers.sparse_featurizer.count_vectors_featurizer\u001b[0m  - 80 vocabulary items were created for text attribute.\n",
      "2024-12-30 20:25:53 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Finished training component 'CountVectorsFeaturizer'.\n",
      "2024-12-30 20:25:53 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Starting to train component 'CountVectorsFeaturizer'.\n",
      "2024-12-30 20:25:53 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.nlu.featurizers.sparse_featurizer.count_vectors_featurizer\u001b[0m  - 697 vocabulary items were created for text attribute.\n",
      "2024-12-30 20:25:53 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Finished training component 'CountVectorsFeaturizer'.\n",
      "2024-12-30 20:25:54 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Starting to train component 'DIETClassifier'.\n",
      "Epochs: 100% 100/100 [00:42<00:00,  2.33it/s, t_loss=1.15, i_acc=1]\n",
      "2024-12-30 20:26:50 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Finished training component 'DIETClassifier'.\n",
      "2024-12-30 20:26:50 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Starting to train component 'EntitySynonymMapper'.\n",
      "2024-12-30 20:26:50 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Finished training component 'EntitySynonymMapper'.\n",
      "2024-12-30 20:26:50 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Starting to train component 'ResponseSelector'.\n",
      "2024-12-30 20:26:50 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.nlu.selectors.response_selector\u001b[0m  - Retrieval intent parameter was left to its default value. This response selector will be trained on training examples combining all retrieval intents.\n",
      "2024-12-30 20:26:50 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Finished training component 'ResponseSelector'.\n",
      "Processed story blocks: 100% 3/3 [00:00<00:00, 2026.89it/s, # trackers=1]\n",
      "Processed story blocks: 100% 3/3 [00:00<00:00, 971.43it/s, # trackers=3]\n",
      "Processed story blocks: 100% 3/3 [00:00<00:00, 265.83it/s, # trackers=12]\n",
      "Processed story blocks: 100% 3/3 [00:00<00:00, 70.43it/s, # trackers=39]\n",
      "Processed rules: 100% 2/2 [00:00<00:00, 2641.25it/s, # trackers=1]\n",
      "2024-12-30 20:26:50 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Starting to train component 'MemoizationPolicy'.\n",
      "Processed trackers: 100% 3/3 [00:00<00:00, 1329.27it/s, # action=12]\n",
      "Processed actions: 12it [00:00, 5129.08it/s, # examples=12]\n",
      "2024-12-30 20:26:50 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Finished training component 'MemoizationPolicy'.\n",
      "2024-12-30 20:26:50 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Starting to train component 'RulePolicy'.\n",
      "Processed trackers: 100% 2/2 [00:00<00:00, 1590.86it/s, # action=5]\n",
      "Processed actions: 5it [00:00, 11137.29it/s, # examples=4]\n",
      "Processed trackers: 100% 3/3 [00:00<00:00, 1967.92it/s, # action=12]\n",
      "Processed trackers: 100% 2/2 [00:00<00:00, 1624.44it/s]\n",
      "Processed trackers: 100% 5/5 [00:00<00:00, 1416.71it/s]\n",
      "2024-12-30 20:26:51 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Finished training component 'RulePolicy'.\n",
      "2024-12-30 20:26:51 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Starting to train component 'TEDPolicy'.\n",
      "Processed trackers: 100% 120/120 [00:00<00:00, 1240.87it/s, # action=30]\n",
      "Epochs: 100% 100/100 [00:22<00:00,  4.52it/s, t_loss=1.96, loss=1.8, acc=1]\n",
      "2024-12-30 20:27:14 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Finished training component 'TEDPolicy'.\n",
      "2024-12-30 20:27:14 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Starting to train component 'UnexpecTEDIntentPolicy'.\n",
      "2024-12-30 20:27:14 \u001b[1;30mWARNING \u001b[0m \u001b[34mrasa.shared.utils.common\u001b[0m  - \u001b[33mThe UnexpecTED Intent Policy is currently experimental and might change or be removed in the future 🔬 Please share your feedback on it in the forum (https://forum.rasa.com) to help us make this feature ready for production.\u001b[0m\n",
      "Processed trackers: 100% 120/120 [00:00<00:00, 3661.95it/s, # intent=12]\n",
      "Epochs: 100% 100/100 [00:23<00:00,  4.17it/s, t_loss=0.134, loss=0.0164, acc=1]\n",
      "2024-12-30 20:27:42 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Finished training component 'UnexpecTEDIntentPolicy'.\n",
      "\u001b[92mYour Rasa model is trained and saved at 'models/20241230-202552-figurative-chronology.tar.gz'.\u001b[0m\n",
      "If you want to speak to the assistant, run 'rasa shell' at any time inside the project directory.\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "# Quick overview of RASA x LanceDB Integration Workflow\n",
    "\n",
    "**Step 1 : Define knowledge_data and store it in LanceDB:**\n",
    "* Use LanceDB to store and manage structured knowledge data relevant to customer support.\n",
    "\n",
    "**Step 2 : Configure Rasa Files:**\n",
    "- nlu.yml: Train intent recognition and entity extraction.\n",
    "- stories.yml & rules.yml: Define conversational flows and rules.\n",
    "- domain.yml: Specify intents, entities, actions, and responses.\n",
    "- config.yml: Set up the NLP pipeline and policies.\n",
    "\n",
    "**Step 3 : Implement Custom Actions (actions.py):**\n",
    "- Create actions that query LanceDB for relevant information based on user intents.\n",
    "- Integrate OpenAI’s LLM to generate nuanced and context-aware responses.\n",
    "\n",
    "**Step 4 : Train the Model (rasa train):**\n",
    "- Compile and optimize the Rasa model based on the latest configurations and training data.\n",
    "\n",
    "**Step 5 : Run Servers:**\n",
    "- Action Server: Executes custom actions.\n",
    "- Rasa Server: Handles user interactions and manages dialogue using the trained model.\n",
    "\n",
    "**Step 6 : Deploy and Test:**\n",
    "- Interact with the chatbot to ensure it accurately understands queries, retrieves information from LanceDB, and generates appropriate responses via OpenAI’s LLM."
   ],
   "metadata": {
    "id": "IFuOxNwrGbd4"
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### Creating and storing the Knowledge base of Company Support in a Lance Table"
   ],
   "metadata": {
    "id": "vC1Ce1j_NxwF"
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Let's create a dataset containing customer support information. We will use this data to populate LanceDB table.\n",
    "\n",
    "It contains a list of dictionaries where each dictionary represents a piece of knowledge or an FAQ entry. Each entry has a \"content\" key corresponding to the support information."
   ],
   "metadata": {
    "id": "oriMmk0BNBG1"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "# This dataset covers various categories such as Account Management, Billing & Payments, Technical Support, Shipping & Delivery, Returns & Refunds, Product Information, and Company Policies.\n",
    "\n",
    "company_support_data = [\n",
    "    # === Account Management ===\n",
    "    {\n",
    "        \"content\": \"To reset your password, navigate to the login page and click on 'Forgot Password'. You'll receive an email with instructions to create a new password.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"To update your account information, log in to your profile and click on 'Edit Profile'. From there, you can change your email, phone number, and other personal details.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"If you want to delete your account, please contact our support team at support@yourcompany.com with the subject line 'Account Deletion Request'. We'll assist you promptly.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"To change your username, go to your account settings, select 'Username', and enter your desired new username. If it's available, the change will be applied immediately.\"\n",
    "    },\n",
    "    # === Billing & Payments ===\n",
    "    {\n",
    "        \"content\": \"We accept various payment methods including Visa, MasterCard, American Express, PayPal, and Apple Pay.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"To view your billing history, log in to your account, navigate to the 'Billing' section, and select 'Billing History'. You'll see a list of all your past transactions.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"If you believe you've been incorrectly charged, please contact our billing support at billing@yourcompany.com with your order ID and details of the discrepancy.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Your subscription will automatically renew on the renewal date unless you cancel it at least 24 hours before the renewal date.\"\n",
    "    },\n",
    "    # === Technical Support ===\n",
    "    {\n",
    "        \"content\": \"If you encounter issues while using our platform, try clearing your browser cache and cookies, then restart your browser.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"For mobile app issues, ensure that you have the latest version of the app installed. You can update the app from the App Store or Google Play Store.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"To report a bug, please email us at support@yourcompany.com with a detailed description of the issue and any relevant screenshots.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Our platform requires JavaScript to be enabled. Please check your browser settings to ensure JavaScript is turned on.\"\n",
    "    },\n",
    "    # === Shipping & Delivery ===\n",
    "    {\n",
    "        \"content\": \"We offer free standard shipping on all orders over $50. Standard shipping typically takes 5-7 business days.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Express shipping options are available at an additional cost. Delivery usually takes 1-3 business days.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"You can track your order status by logging into your account and visiting the 'Orders' section. Click on your order to see detailed tracking information.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"If your package is delayed, please contact our shipping department at shipping@yourcompany.com with your order number for assistance.\"\n",
    "    },\n",
    "    # === Returns & Refunds ===\n",
    "    {\n",
    "        \"content\": \"You can return most items within 30 days of purchase. Items must be in original condition and packaging.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"To initiate a return, log in to your account, go to the 'Orders' section, select the order you wish to return, and click on 'Start Return'. Follow the on-screen instructions.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Refunds are processed within 7-10 business days after we receive your returned item. The refund will be issued to your original payment method.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Please note that shipping costs are non-refundable unless the return is due to a defect or error on our part.\"\n",
    "    },\n",
    "    # === Order Management ===\n",
    "    {\n",
    "        \"content\": \"To modify your order after placing it, please contact our support team at support@yourcompany.com as soon as possible. Changes can only be made within one hour of order placement.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"If you received the wrong item, please contact us immediately with your order number and a photo of the received product. We'll arrange for a replacement or refund.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"To cancel your order, log in to your account, go to the 'Orders' section, select the order you wish to cancel, and click on 'Cancel Order'.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Parts of your order may ship separately. You will receive separate tracking information for each shipment.\"\n",
    "    },\n",
    "    # === Account Security ===\n",
    "    {\n",
    "        \"content\": \"For enhanced security, enable two-factor authentication (2FA) in your account settings. This adds an extra layer of protection to your account.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"If you suspect unauthorized access to your account, change your password immediately and contact our support team.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Never share your account credentials with anyone. We will never ask for your password via email or phone.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Regularly review your account activity to ensure all actions are authorized. You can view your recent activity in the 'Account Activity' section.\"\n",
    "    },\n",
    "    # === Shipping Issues ===\n",
    "    {\n",
    "        \"content\": \"If your package hasn't arrived within the estimated delivery time, please use the tracking number in your order details to check its status, or contact our shipping support at shipping@yourcompany.com.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"For international shipments, please note that customs processing times may affect delivery dates. We are not responsible for delays caused by customs.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Ensure that your shipping address is correct before placing an order. Incorrect addresses may lead to delivery delays or package returns.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"We offer shipment insurance for an additional fee. This covers lost or damaged packages during transit. To add insurance, select the option during checkout.\"\n",
    "    },\n",
    "    # === Product Availability ===\n",
    "    {\n",
    "        \"content\": \"If a product is out of stock, you can sign up for restock notifications on the product page. We'll notify you via email once it's available again.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Limited edition items are subject to availability. We recommend placing your orders early to secure these exclusive products.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Pre-orders are accepted for upcoming releases. Pre-order now to ensure you receive the product as soon as it ships.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Sold-out items cannot be purchased until new stock arrives. Please check back regularly or join our waitlist.\"\n",
    "    },\n",
    "    # === Shipping Costs ===\n",
    "    {\n",
    "        \"content\": \"Shipping costs vary based on your location and the size of your order. You can view the estimated shipping cost during the checkout process.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"We offer free standard shipping on orders over $75. Orders below this threshold will incur standard shipping fees.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Express shipping is available for an additional fee. If you need your order faster, select the express option at checkout.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Please note that international shipping fees will apply for orders shipped outside the United States.\"\n",
    "    },\n",
    "    # === Subscription Services ===\n",
    "    {\n",
    "        \"content\": \"Subscribe to our monthly box to receive exclusive products and discounts. You can manage your subscription in your account settings.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Canceling your subscription can be done at any time. Please visit the 'Subscriptions' section in your account to make changes.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Subscribers receive early access to new products and special promotions throughout the year.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Pause your subscription if you need a break. You can resume it whenever you're ready by logging into your account.\"\n",
    "    },\n",
    "    # === Order Tracking ===\n",
    "    {\n",
    "        \"content\": \"To track your order, log in to your account, go to the 'Orders' section, and click on 'Track Order' next to the relevant order.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"You'll receive email notifications with tracking updates once your order has been shipped.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"If you haven't received your tracking information, please check your spam folder or contact our support team.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"For real-time tracking, use the tracking number provided and visit the carrier's website directly.\"\n",
    "    },\n",
    "    # === Account Reactivation ===\n",
    "    {\n",
    "        \"content\": \"If you've previously closed your account and wish to reactivate it, please contact our support team at support@yourcompany.com with your account details.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Reactivating your account will restore your previous order history and saved preferences.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Please note that some data may have been permanently deleted if the account was closed more than six months ago.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Upon reactivation, you may need to update your password and verify your email address for security purposes.\"\n",
    "    },\n",
    "    # === Payment Issues ===\n",
    "    {\n",
    "        \"content\": \"If your payment is declined, please verify that your billing information is correct and that your card has sufficient funds.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"For any issues with payments made through PayPal, please ensure your account is in good standing and that your linked bank accounts are active.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Expired or invalid credit cards cannot be used. Please update your payment method in your account settings.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"If you've been double-charged, contact our billing support at billing@yourcompany.com with your order ID and transaction details.\"\n",
    "    },\n",
    "    # === Warranty Information ===\n",
    "    {\n",
    "        \"content\": \"All our products come with a one-year warranty covering manufacturing defects and hardware malfunctions.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"To claim a warranty, contact our support team with your order number and a description of the issue.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Warranty does not cover accidental damage, unauthorized repairs, or normal wear and tear.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Extended warranty options are available at the time of purchase for an additional fee.\"\n",
    "    },\n",
    "    # === Privacy & Data Security ===\n",
    "    {\n",
    "        \"content\": \"We prioritize your privacy and ensure that your personal data is protected. Please refer to our Privacy Policy for detailed information.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"You have control over your personal information. Manage your privacy settings in the 'Account Settings' section.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"We do not share your personal information with third parties without your explicit consent, except as required by law.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"For any concerns about data security or privacy, contact our privacy team at privacy@yourcompany.com.\"\n",
    "    },\n",
    "    # === Shipping Restrictions ===\n",
    "    {\n",
    "        \"content\": \"We currently do not ship to P.O. Boxes or APO/FPO addresses. Please provide a valid residential or business address.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Certain products may have shipping restrictions based on international regulations. Check the product page for any specific restrictions.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Hazardous materials and perishable items have special shipping requirements. Ensure compliance by reviewing our shipping guidelines.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Import duties and taxes are the responsibility of the recipient for international orders. These fees are not included in the shipping cost.\"\n",
    "    },\n",
    "    # === Frequently Asked Questions (FAQs) ===\n",
    "    {\n",
    "        \"content\": \"Q: How can I track my order?\\nA: You can track your order by logging into your account, navigating to the 'Orders' section, and clicking on 'Track Order' next to your order.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Q: What is your return policy?\\nA: We accept returns within 30 days of purchase. Items must be in original condition and packaging. To initiate a return, visit the 'Orders' section in your account.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Q: Do you offer international shipping?\\nA: Yes, we ship to select countries internationally. Shipping fees and delivery times may vary based on location.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Q: How do I change my shipping address?\\nA: To change your shipping address, go to your account settings, click on 'Edit Address', and update your details. Note that changes can only be made before the order is shipped.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Q: Can I cancel my order?\\nA: Yes, you can cancel your order within one hour of placing it. To cancel, go to the 'Orders' section in your account and select 'Cancel Order'.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Q: How secure is my personal information?\\nA: We use industry-standard encryption and security measures to protect your personal information. Refer to our Privacy Policy for more details.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Q: What payment methods are accepted?\\nA: We accept Visa, MasterCard, American Express, PayPal, Apple Pay, and Google Pay.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Q: How do I apply a promo code?\\nA: During checkout, enter your promo code in the 'Promo Code' field and click 'Apply' to receive your discount.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Q: Who do I contact for support?\\nA: For any inquiries or assistance, contact our support team at support@yourcompany.com or call us at (123) 456-7890.\"\n",
    "    },\n",
    "    # === Account Verification ===\n",
    "    {\n",
    "        \"content\": \"To verify your account, check your email for a verification link after signing up. Click on the link to activate your account.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"If you did not receive a verification email, please check your spam folder or resend the verification email from your account settings.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Account verification is required to access all features of our platform, including order placement and account management.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"For any issues with account verification, contact our support team at support@yourcompany.com.\"\n",
    "    },\n",
    "    # === Contact Information ===\n",
    "    {\n",
    "        \"content\": \"You can reach our support team via email at support@yourcompany.com or call us at (123) 456-7890 from Monday to Friday, 9 AM to 5 PM EST.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Follow us on social media for the latest updates, promotions, and customer support: Facebook, Twitter, Instagram.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Visit our 'Contact Us' page on our website for a direct contact form and additional support options.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Our headquarters are located at 1234 Business Ave, Suite 100, City, State, ZIP Code.\"\n",
    "    },\n",
    "    # === Order Confirmation ===\n",
    "    {\n",
    "        \"content\": \"After placing an order, you'll receive a confirmation email with your order details and estimated delivery date.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"If you haven't received your order confirmation, please check your spam folder or contact our support team for assistance.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"The order confirmation email includes a summary of your purchase, billing information, and contact details for support.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"For large orders or bulk purchases, you'll receive a separate confirmation email with additional details.\"\n",
    "    },\n",
    "    # === Shipping Options ===\n",
    "    {\n",
    "        \"content\": \"We offer three shipping options: Standard, Express, and Overnight. Choose the one that best fits your needs during checkout.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Standard shipping is free for orders over $75. It typically takes 5-7 business days for delivery.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Express shipping costs are calculated based on the weight and destination of your order. Estimated delivery is 2-3 business days.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Overnight shipping is available for urgent orders. Placing your order before 10 AM ensures next-day delivery.\"\n",
    "    },\n",
    "    # === Custom Order Handling ===\n",
    "    {\n",
    "        \"content\": \"For custom orders, please contact our support team at support@yourcompany.com with detailed specifications and requirements.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Custom orders may require additional processing time. Delivery dates will be provided after discussing your specific needs.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Pricing for custom orders varies based on the complexity and materials involved. Our support team will provide a quote upon request.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Please review all custom order details carefully before finalizing to ensure accuracy and satisfaction.\"\n",
    "    },\n",
    "    # === FAQs Specific ===\n",
    "    {\n",
    "        \"content\": \"Q: How do I track my order?\\nA: You can track your order by logging into your account, going to the 'Orders' section, and clicking on 'Track Order' next to your recent purchase.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Q: What is your return policy?\\nA: We accept returns within 30 days of purchase. Items must be in their original condition and packaging. To initiate a return, visit the 'Orders' section in your account and select 'Return Item'.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Q: Do you offer international shipping?\\nA: Yes, we ship to select countries internationally. Shipping fees and delivery times vary based on your location. Please refer to the 'Shipping Information' page for more details.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Q: How can I contact customer support?\\nA: You can contact our support team via email at support@yourcompany.com or call us at (123) 456-7890 during our support hours, Monday to Friday, 9 AM to 5 PM EST.\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Q: What payment methods do you accept?\\nA: We accept Visa, MasterCard, American Express, PayPal, Apple Pay, and Google Pay.\"\n",
    "    },\n",
    "    # ... Add more FAQs as needed\n",
    "]"
   ],
   "metadata": {
    "id": "xEJy6e3t6iD1"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": [
    "Let's set up a robust knowledge base for an advanced customer support chatbot by leveraging LanceDB for efficient data storage and retrieval, and a Sentence Transformer model for generating vector embeddings.\n",
    "\n",
    "We will be using pre-trained Sentence Transformer model (BAAI/bge-small-en-v1.5) to convert textual support data into vector embeddings. You can change it accordingly.\n",
    "\n",
    "Then we will create a table - \"knowledge_base\" in the DB and insert all the customer support information in this table."
   ],
   "metadata": {
    "id": "f3mhGwnfLN4H"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "# Import necessary libraries\n",
    "import os\n",
    "import subprocess\n",
    "import time\n",
    "import threading\n",
    "import lancedb\n",
    "import requests\n",
    "import json\n",
    "from lancedb.pydantic import LanceModel, Vector\n",
    "from lancedb.embeddings import get_registry"
   ],
   "metadata": {
    "id": "TDf3zfjS6iGm"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "source": [
    "# Initialize LanceDB\n",
    "db = lancedb.connect(\"./content/lancedb\")  # Local storage within Colab\n",
    "\n",
    "# Initialize the language model for generating embeddings\n",
    "model = (\n",
    "    get_registry()\n",
    "    .get(\"sentence-transformers\")\n",
    "    .create(name=\"BAAI/bge-small-en-v1.5\", device=\"cpu\")\n",
    ")\n",
    "\n",
    "\n",
    "# Create table from schema\n",
    "class Documents(LanceModel):\n",
    "    vector: Vector(model.ndims()) = model.VectorField()\n",
    "    content: str = model.SourceField()  # Field to store the actual content/response\n",
    "\n",
    "\n",
    "# Knowledge data from the data above\n",
    "knowledge_data = company_support_data\n",
    "\n",
    "# Define table name\n",
    "table_name = \"knowledge_base\"\n",
    "\n",
    "# Retrieve existing table names\n",
    "existing_tables = db.table_names()\n",
    "\n",
    "if table_name not in existing_tables:\n",
    "    # Create a new table with the schema and insert data\n",
    "    tbl = db.create_table(table_name, schema=Documents)\n",
    "    tbl.add(knowledge_data)\n",
    "    print(f\"Created new table '{table_name}' and inserted data.\")\n",
    "else:\n",
    "    # Append data to the existing table\n",
    "    table = db.open_table(table_name)\n",
    "    table.add(knowledge_data, mode=\"overwrite\")\n",
    "    print(f\"Overwrited data to the existing table '{table_name}'.\")"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "kS6SZJYx6iJZ",
    "outputId": "8f7b818f-0b15-4c2a-c9a3-2e4b3caa16f1"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Overwrited data to the existing table 'knowledge_base'.\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "###  Configure Rasa Files"
   ],
   "metadata": {
    "id": "SSc4gFYRWBjl"
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### domain.yml\n",
    "The domain.yml file serves as the core configuration for your Rasa chatbot. It defines the chatbot’s intents, entities, slots, responses, actions, forms, and policies.\n",
    "\n",
    "It has the following Components -\n",
    "1. **Intents**: Represents the purposes behind user messages (e.g., greetings, inquiries about account deletion).\n",
    "2. **Entities**: Specific pieces of information extracted from user inputs (e.g., project names, dates).\n",
    "3. **Responses**: Predefined messages the chatbot can send back to users.\n",
    "4. **Actions**: Lists both built-in and custom actions that the chatbot can perform.\n",
    "5. **Slots**: Variables that store information during a conversation to maintain context and continuity.\n",
    "\n"
   ],
   "metadata": {
    "id": "EzRfZD3mB6HI"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "%%writefile domain.yml\n",
    "version: \"3.0\"\n",
    "\n",
    "language: \"en\"\n",
    "\n",
    "intents:\n",
    "  - greet\n",
    "  - ask_knowledge\n",
    "  - goodbye\n",
    "\n",
    "entities:\n",
    "  - project\n",
    "  - service\n",
    "\n",
    "responses:\n",
    "  utter_greet:\n",
    "    - text: \"Hello! How can I assist you today?\"\n",
    "\n",
    "  utter_goodbye:\n",
    "    - text: \"Goodbye! Have a great day!\"\n",
    "    - text: \"Bye! Let me know if you need anything else.\"\n",
    "    - text: \"See you later! Feel free to reach out anytime.\"\n",
    "\n",
    "actions:\n",
    "  - action_search_knowledge\n"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "jYTu3y516iOr",
    "outputId": "155f888a-5fe1-486f-ca27-b53dd5ff93be"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Overwriting domain.yml\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### endpoints.yml\n",
    "The endpoints.yml file configures various backend services that Rasa interacts with, such as action servers, event brokers, and tracker stores. It specifies the URLs and connection details for services like the custom action server (actions.py), enabling Rasa to communicate with it.\n",
    "\n",
    "Following configurations are relevant to this use-case -\n",
    "1. **Action Endpoint**: Defines where Rasa should send requests to execute custom actions, typically pointing to the server running actions.py.\n",
    "2. **Tracker Store**: (Optional) Configures how conversation states are stored, which can be essential for maintaining session information in more complex setups."
   ],
   "metadata": {
    "id": "iLNhVSayCnc3"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "%%writefile endpoints.yml\n",
    "# This file contains the different endpoints your bot can use.\n",
    "\n",
    "# Server where the models are pulled from.\n",
    "# https://rasa.com/docs/rasa/model-storage#fetching-models-from-a-server\n",
    "\n",
    "#models:\n",
    "#  url: http://my-server.com/models/default_core@latest\n",
    "#  wait_time_between_pulls:  10   # [optional](default: 100)\n",
    "\n",
    "# Server which runs your custom actions.\n",
    "# https://rasa.com/docs/rasa/custom-actions\n",
    "\n",
    "action_endpoint:\n",
    "  url: \"http://localhost:5055/webhook\"\n",
    "\n",
    "# Tracker store which is used to store the conversations.\n",
    "# By default the conversations are stored in memory.\n",
    "# https://rasa.com/docs/rasa/tracker-stores\n",
    "\n",
    "#tracker_store:\n",
    "#    type: redis\n",
    "#    url: <host of the redis instance, e.g. localhost>\n",
    "#    port: <port of your redis instance, usually 6379>\n",
    "#    db: <number of your database within redis, e.g. 0>\n",
    "#    password: <password used for authentication>\n",
    "#    use_ssl: <whether or not the communication is encrypted, default false>\n",
    "\n",
    "#tracker_store:\n",
    "#    type: mongod\n",
    "#    url: <url to your mongo instance, e.g. mongodb://localhost:27017>\n",
    "#    db: <name of the db within your mongo instance, e.g. rasa>\n",
    "#    username: <username used for authentication>\n",
    "#    password: <password used for authentication>\n",
    "\n",
    "# Event broker which all conversation events should be streamed to.\n",
    "# https://rasa.com/docs/rasa/event-brokers\n",
    "\n",
    "#event_broker:\n",
    "#  url: localhost\n",
    "#  username: username\n",
    "#  password: password\n",
    "#  queue: queue\n"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "9SHFq76-6iRm",
    "outputId": "d1fd82c5-01ad-46f2-9ec9-2e5265e00941"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Overwriting endpoints.yml\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### stories.yml\n",
    "\n",
    "The stories.yml file contains training stories that define example conversational paths your chatbot can take. These stories help Rasa understand how to manage dialogues by learning from example interactions. It provides Rasa with sequences of user intents and corresponding actions, teaching it how to handle various conversational scenarios.\n"
   ],
   "metadata": {
    "id": "Hi8C-oeNC85g"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "%%writefile data/stories.yml\n",
    "version: \"3.0\"\n",
    "\n",
    "stories:\n",
    "  - story: Greet and ask question\n",
    "    steps:\n",
    "      - intent: greet\n",
    "      - action: utter_greet\n",
    "      - intent: ask_knowledge\n",
    "      - action: action_search_knowledge\n",
    "\n",
    "  - story: ask question\n",
    "    steps:\n",
    "      - intent: ask_knowledge\n",
    "      - action: action_search_knowledge\n",
    "\n",
    "  - story: Goodbye\n",
    "    steps:\n",
    "      - intent: goodbye\n",
    "      - action: utter_goodbye\n",
    "\n",
    "  - story: greet and goodbye\n",
    "    steps:\n",
    "      - intent: greet\n",
    "      - action: utter_greet\n",
    "      - intent: goodbye\n",
    "      - action: utter_goodbye"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "UUqa6mZb_x0m",
    "outputId": "590517d2-0412-405d-dd54-81d0976824cd"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Overwriting data/stories.yml\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### rules.yml\n",
    "The rules.yml file defines rule-based conversations that specify exact steps the chatbot should follow in certain situations. Unlike stories, rules are strict paths that Rasa should follow without deviation. It ensures that in specific scenarios, the chatbot responds in a predefined manner, which is crucial for critical interactions like error handling or confirmations.\n",
    "\n",
    "It works alongside stories to cover fixed conversational flows that require precise control, such as security checks or policy confirmations."
   ],
   "metadata": {
    "id": "RdGtoB13DlMe"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "%%writefile data/rules.yml\n",
    "version: \"3.0\"\n",
    "\n",
    "rules:\n",
    "  - rule: Greet\n",
    "    steps:\n",
    "      - intent: greet\n",
    "      - action: utter_greet\n",
    "\n",
    "  - rule: Goodbye\n",
    "    steps:\n",
    "      - intent: goodbye\n",
    "      - action: utter_goodbye\n",
    "\n",
    "  - rule: Answer Knowledge Questions\n",
    "    steps:\n",
    "      - intent: ask_knowledge\n",
    "      - action: action_search_knowledge\n"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "pI0yN537_x3K",
    "outputId": "8b7bfec5-3522-4e41-98cd-4e4cb385f5b2"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Overwriting data/rules.yml\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### nlu.yml\n",
    "The nlu.yml file contains Natural Language Understanding (NLU) training data. It includes examples of user inputs categorized by intents and annotated with entities to train Rasa’s NLU component.\n",
    "\n",
    "It has the following role in the project -\n",
    "1. **Intent Recognition**: Enables Rasa to accurately identify the user’s intent from their message.\n",
    "2. **Entity Extraction**: Allows Rasa to extract specific information from user inputs, which can be used in custom actions to query LanceDB or generate responses via OpenAI’s LLM.\n",
    "3. **Training Data Quality**: The quality and diversity of examples in nlu.yml directly impact the chatbot’s ability to understand and respond appropriately to a wide range of user queries."
   ],
   "metadata": {
    "id": "CV1KKmQyD9VB"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "%%writefile data/nlu.yml\n",
    "# data/nlu.yml\n",
    "\n",
    "version: \"3.0\"\n",
    "\n",
    "nlu:\n",
    "  - intent: greet\n",
    "    examples: |\n",
    "      - hello\n",
    "      - hi\n",
    "      - hey\n",
    "      - good morning\n",
    "      - good evening\n",
    "      - greetings\n",
    "\n",
    "  - intent: goodbye\n",
    "    examples: |\n",
    "      - bye\n",
    "      - goodbye\n",
    "      - see you later\n",
    "      - catch you later\n",
    "      - see ya\n",
    "      - take care\n",
    "\n",
    "  - intent: ask_knowledge\n",
    "    examples: |\n",
    "      - I need help with my account\n",
    "      - Can you assist me with billing?\n",
    "      - How do I reset my password?\n",
    "      - I'm facing issues with my order\n",
    "      - Tell me about your support services\n",
    "      - How can I contact customer service?\n",
    "      - What are your support hours?\n",
    "      - I have a question about Project Alpha\n",
    "      - Help me understand Project Beta\n",
    "      - How can I track my purchase?\n"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "yhAW5Eh1_x5-",
    "outputId": "b474f20e-9f7e-4dd6-ff2c-1ba12542ceb9"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Overwriting data/nlu.yml\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### config.yml\n",
    "\n",
    "The config.yml file defines the pipeline and policies used by Rasa for processing natural language inputs and managing dialogue workflows.\n",
    "\n",
    "Let us understand the components of the config.yml file\n",
    "1. **NLP Pipeline**: Defines the components that handle preprocessing, feature extraction, intent classification, and entity recognition.\n",
    "2. **Policies**: Dictate how the chatbot selects actions based on the current dialogue state, using rules, machine learning models, or a combination of both.\n",
    "3. **Endpoint Integrations**: Can include configurations for connecting to external services or APIs, enhancing the chatbot’s capabilities.\n",
    "\n",
    "It specifies the sequence of components (like tokenizers, featurizers, classifiers) that process user inputs for intent recognition and entity extraction. Then it determines how Rasa decides the next action based on the conversation state and the trained model."
   ],
   "metadata": {
    "id": "IDYPv5bPEnhb"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "%%writefile config.yml\n",
    "# config.yml\n",
    "version: \"3.0\"\n",
    "\n",
    "language: \"en\"\n",
    "\n",
    "pipeline:\n",
    "- name: WhitespaceTokenizer\n",
    "- name: RegexFeaturizer\n",
    "- name: LexicalSyntacticFeaturizer\n",
    "- name: CountVectorsFeaturizer\n",
    "- name: CountVectorsFeaturizer\n",
    "  analyzer: char_wb\n",
    "  min_ngram: 1\n",
    "  max_ngram: 4\n",
    "- name: DIETClassifier\n",
    "  epochs: 100\n",
    "- name: EntitySynonymMapper\n",
    "- name: ResponseSelector\n",
    "  epochs: 100\n",
    "\n",
    "policies:\n",
    "- name: RulePolicy\n",
    "- name: UnexpecTEDIntentPolicy\n",
    "  max_history: 5\n",
    "  epochs: 100\n",
    "- name: TEDPolicy\n",
    "  max_history: 5\n",
    "  epochs: 100\n",
    "assistant_id: 20241227-151505-young-attachment"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "KDYvRppw_x8s",
    "outputId": "9ddd9edb-f49e-4456-d8bd-379c18c6e0fb"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Overwriting config.yml\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "### Implement Custom Actions (actions.py) file\n",
    "\n",
    "The actions.py file is where you define custom actions for your Rasa chatbot. Custom actions are Python functions that can execute arbitrary logic, such as querying a database, calling external APIs, processing data, or integrating with services like LanceDB and OpenAI’s LLMs.\n",
    "\n",
    "We will be doing the following in this file -\n",
    "1. **Get user input**: Processes user inputs and proceed to next step.\n",
    "2. **Interacting with Databases**: Queries LanceDB to retrieve relevant information based on user queries.\n",
    "3.**Enhancing Responses with LLMs**: Utilizes OpenAI’s API to generate detailed, contextually relevant responses that go beyond predefined replies."
   ],
   "metadata": {
    "id": "48EX9SfzBQ_M"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "%%writefile actions/actions.py\n",
    "from typing import Any, Text, Dict, List\n",
    "from rasa_sdk import Action, Tracker\n",
    "from rasa_sdk.executor import CollectingDispatcher\n",
    "import lancedb\n",
    "import logging\n",
    "from google.colab import userdata\n",
    "import openai\n",
    "import os\n",
    "from dotenv import load_dotenv\n",
    "\n",
    "# Load environment variables from .env\n",
    "load_dotenv()\n",
    "\n",
    "# Configure logging\n",
    "logger = logging.getLogger(__name__)\n",
    "logging.basicConfig(level=logging.INFO)\n",
    "\n",
    "class ActionSearchKnowledge(Action):\n",
    "    def name(self) -> Text:\n",
    "        return \"action_search_knowledge\"\n",
    "\n",
    "    def __init__(self):\n",
    "\n",
    "        # Initialize OpenAI API key from environment variables\n",
    "        self.openai_api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "        if not self.openai_api_key:\n",
    "            logger.error(\"OpenAI API key not found. Please set OPENAI_API_KEY in your environment.\")\n",
    "        openai.api_key = self.openai_api_key\n",
    "\n",
    "        # Initialize LanceDB connection once\n",
    "        try:\n",
    "            self.db = lancedb.connect(\"./content/lancedb\")\n",
    "            self.table_name = \"knowledge_base\"\n",
    "            if self.table_name not in self.db.table_names():\n",
    "                logger.error(f\"Table '{self.table_name}' does not exist in LanceDB.\")\n",
    "                self.table = None\n",
    "            else:\n",
    "                self.table = self.db.open_table(self.table_name)\n",
    "                logger.info(f\"Connected to table '{self.table_name}' in LanceDB.\")\n",
    "        except Exception as e:\n",
    "            logger.error(f\"Error connecting to LanceDB: {e}\")\n",
    "            self.table = None\n",
    "\n",
    "    def run(self, dispatcher: CollectingDispatcher,\n",
    "            tracker: Tracker,\n",
    "            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:\n",
    "\n",
    "\n",
    "        # Get the latest user message\n",
    "        user_message = tracker.latest_message.get('text')\n",
    "        logger.info(f\"User message: {user_message}\")\n",
    "\n",
    "        if not user_message:\n",
    "            dispatcher.utter_message(text=\"Sorry, I didn't catch that. Could you please repeat?\")\n",
    "            return []\n",
    "\n",
    "        try:\n",
    "            # Perform similarity search in LanceDB\n",
    "            query_result = self.table.search(user_message).limit(1).to_pandas()\n",
    "\n",
    "            # Filter results based on the _distance parameter (smaller _distance means more similar)\n",
    "            relevant_content = [query_result.loc[0, \"content\"] if query_result.loc[0, \"_distance\"] < 0.65 else None][0]\n",
    "            response_text = \"Null\"\n",
    "\n",
    "            # If we find relevant content , sent it to LLM or Else send automated reply\n",
    "            if not relevant_content == None:\n",
    "                logger.info(f\"Retrieved answer from knowledge base.\")\n",
    "                # Use OpenAI to generate a more refined response\n",
    "                response_text = self.generate_response(user_message, relevant_content)\n",
    "            else:\n",
    "                # If user has ask not a relevant question, reply with the following\n",
    "                response_text = \"I'm sorry, I don't have an answer to that question.\"\n",
    "                logger.info(f\"No matching content found in knowledge base.\")\n",
    "\n",
    "            # Send the answer back to the user\n",
    "            dispatcher.utter_message(text=response_text)\n",
    "\n",
    "        except Exception as e:\n",
    "            logger.error(f\"Error during search operation: {e}\")\n",
    "            dispatcher.utter_message(text=\"Sorry, something went wrong while processing your request.\")\n",
    "\n",
    "        return []\n",
    "\n",
    "    def generate_response(self, user_message: Text, relevant_content: Text) -> Text:\n",
    "        \"\"\"\n",
    "        Use OpenAI's API to generate a refined response based on user message and relevant content.\n",
    "        \"\"\"\n",
    "        try:\n",
    "            system_prompt = \"You are an company support assistant that provides helpful and accurate answers based on the provided information. You talk professionally and like a customer support executive.\"\n",
    "\n",
    "            prompt = (\n",
    "                f\"User Question: {user_message}\\n\"\n",
    "                f\"Relevant Information: {relevant_content}\\n\\n\"\n",
    "                f\"Provide a detailed and helpful response to the user's question based on the relevant information above.\"\n",
    "            )\n",
    "\n",
    "            response = openai.ChatCompletion.create(\n",
    "                model=\"gpt-4o-mini\",\n",
    "                messages=[\n",
    "                    {\"role\": \"system\", \"content\": system_prompt},\n",
    "                    {\"role\": \"user\", \"content\": prompt}\n",
    "                ],\n",
    "                max_tokens=450,\n",
    "                temperature=0.7,\n",
    "            )\n",
    "\n",
    "            generated_text = response.choices[0].message['content'].strip()\n",
    "            logger.info(\"Generated response using OpenAI API.\")\n",
    "            return generated_text\n",
    "\n",
    "        except Exception as e:\n",
    "            logger.error(f\"Error generating response with OpenAI API: {e}\")\n",
    "            return relevant_content  # Fallback to relevant content if OpenAI fails"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "eenvEl8y6iL-",
    "outputId": "f934500b-b4a9-4bd7-e0c8-743fbaf1f3d7"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Overwriting actions/actions.py\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "### Training RASA Model\n",
    "The rasa train command is used to train the Rasa model based on the data and configurations defined in your project files (nlu.yml, stories.yml, rules.yml, etc.)\n",
    "\n",
    "It compiles the training data into a machine learning model that Rasa uses to interpret user inputs and manage conversations. The training process optimizes the model’s ability to accurately recognize intents, extract entities, and decide on appropriate actions, which is crucial for integrating seamless interactions with LanceDB and generating responses via LLMs."
   ],
   "metadata": {
    "id": "NZyBVLzLFY0Z"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "# Train the Rasa model\n",
    "!rasa train"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "6FmKesSV_x_e",
    "outputId": "2b87edf2-8a78-4c26-9845-688087eae709"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "/usr/local/lib/python3.10/dist-packages/rasa/core/tracker_store.py:1044: MovedIn20Warning: \u001b[31mDeprecated API features detected! These feature(s) are not compatible with SQLAlchemy 2.0. \u001b[32mTo prevent incompatible upgrades prior to updating applications, ensure requirements files are pinned to \"sqlalchemy<2.0\". \u001b[36mSet environment variable SQLALCHEMY_WARN_20=1 to show all deprecation warnings.  Set environment variable SQLALCHEMY_SILENCE_UBER_WARNING=1 to silence this message.\u001b[0m (Background on SQLAlchemy 2.0 at: https://sqlalche.me/e/b8d9)\n",
      "  Base: DeclarativeMeta = declarative_base()\n",
      "/usr/local/lib/python3.10/dist-packages/rasa/shared/utils/validation.py:134: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html\n",
      "  import pkg_resources\n",
      "/usr/local/lib/python3.10/dist-packages/pkg_resources/__init__.py:3154: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('google')`.\n",
      "Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages\n",
      "  declare_namespace(pkg)\n",
      "/usr/local/lib/python3.10/dist-packages/pkg_resources/__init__.py:3154: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('google.cloud')`.\n",
      "Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages\n",
      "  declare_namespace(pkg)\n",
      "/usr/local/lib/python3.10/dist-packages/pkg_resources/__init__.py:3154: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('mpl_toolkits')`.\n",
      "Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages\n",
      "  declare_namespace(pkg)\n",
      "/usr/local/lib/python3.10/dist-packages/pkg_resources/__init__.py:3154: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('ruamel')`.\n",
      "Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages\n",
      "  declare_namespace(pkg)\n",
      "/usr/local/lib/python3.10/dist-packages/pkg_resources/__init__.py:3154: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('sphinxcontrib')`.\n",
      "Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages\n",
      "  declare_namespace(pkg)\n",
      "2024-12-30 21:14:02 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.cli.train\u001b[0m  - Started validating domain and training data...\n",
      "/usr/local/lib/python3.10/dist-packages/tensorflow/lite/python/util.py:52: DeprecationWarning: jax.xla_computation is deprecated. Please use the AOT APIs.\n",
      "  from jax import xla_computation as _xla_computation\n",
      "2024-12-30 21:14:07 \u001b[1;30mINFO    \u001b[0m \u001b[34mnumexpr.utils\u001b[0m  - NumExpr defaulting to 2 threads.\n",
      "\u001b[93m/usr/local/lib/python3.10/dist-packages/rasa/shared/utils/io.py:99: UserWarning: Training data file /content/domain.yml has a lower format version than your Rasa Open Source installation: 3.0 < 3.1. Rasa Open Source will read the file as a version 3.1 file. Please update your version key to 3.1. See https://rasa.com/docs/rasa/training-data-format.\n",
      "\u001b[0m\u001b[93m/usr/local/lib/python3.10/dist-packages/rasa/shared/utils/io.py:99: UserWarning: Training data file /content/data/nlu.yml has a lower format version than your Rasa Open Source installation: 3.0 < 3.1. Rasa Open Source will read the file as a version 3.1 file. Please update your version key to 3.1. See https://rasa.com/docs/rasa/training-data-format.\n",
      "\u001b[0m\u001b[93m/usr/local/lib/python3.10/dist-packages/rasa/shared/utils/io.py:99: UserWarning: Training data file /content/data/rules.yml has a lower format version than your Rasa Open Source installation: 3.0 < 3.1. Rasa Open Source will read the file as a version 3.1 file. Please update your version key to 3.1. See https://rasa.com/docs/rasa/training-data-format.\n",
      "\u001b[0m\u001b[93m/usr/local/lib/python3.10/dist-packages/rasa/shared/utils/io.py:99: UserWarning: Training data file /content/data/stories.yml has a lower format version than your Rasa Open Source installation: 3.0 < 3.1. Rasa Open Source will read the file as a version 3.1 file. Please update your version key to 3.1. See https://rasa.com/docs/rasa/training-data-format.\n",
      "\u001b[0m2024-12-30 21:14:08 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.validator\u001b[0m  - Validating intents...\n",
      "2024-12-30 21:14:08 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.validator\u001b[0m  - Validating uniqueness of intents and stories...\n",
      "2024-12-30 21:14:08 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.validator\u001b[0m  - Validating utterances...\n",
      "2024-12-30 21:14:08 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.validator\u001b[0m  - Story structure validation...\n",
      "Processed story blocks: 100% 4/4 [00:00<00:00, 1979.38it/s, # trackers=1]\n",
      "2024-12-30 21:14:08 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.core.training.story_conflict\u001b[0m  - Considering all preceding turns for conflict analysis.\n",
      "2024-12-30 21:14:08 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.validator\u001b[0m  - No story structure conflicts found.\n",
      "\u001b[93m/usr/local/lib/python3.10/dist-packages/rasa/shared/utils/io.py:99: UserWarning: Training data file /content/domain.yml has a lower format version than your Rasa Open Source installation: 3.0 < 3.1. Rasa Open Source will read the file as a version 3.1 file. Please update your version key to 3.1. See https://rasa.com/docs/rasa/training-data-format.\n",
      "\u001b[0m\u001b[93m/usr/local/lib/python3.10/dist-packages/rasa/shared/utils/io.py:99: UserWarning: Training data file /content/data/rules.yml has a lower format version than your Rasa Open Source installation: 3.0 < 3.1. Rasa Open Source will read the file as a version 3.1 file. Please update your version key to 3.1. See https://rasa.com/docs/rasa/training-data-format.\n",
      "\u001b[0m\u001b[93m/usr/local/lib/python3.10/dist-packages/rasa/shared/utils/io.py:99: UserWarning: Training data file /content/data/stories.yml has a lower format version than your Rasa Open Source installation: 3.0 < 3.1. Rasa Open Source will read the file as a version 3.1 file. Please update your version key to 3.1. See https://rasa.com/docs/rasa/training-data-format.\n",
      "\u001b[0m\u001b[93m/usr/local/lib/python3.10/dist-packages/rasa/shared/utils/io.py:99: UserWarning: Training data file /content/data/nlu.yml has a lower format version than your Rasa Open Source installation: 3.0 < 3.1. Rasa Open Source will read the file as a version 3.1 file. Please update your version key to 3.1. See https://rasa.com/docs/rasa/training-data-format.\n",
      "\u001b[0m\u001b[93m/usr/local/lib/python3.10/dist-packages/rasa/engine/recipes/recipe.py:35: FutureWarning: From Rasa Open Source 4.0.0 onwards it will be required to specify a recipe in your model configuration. Defaulting to recipe 'default.v1'.\n",
      "  rasa.shared.utils.io.raise_deprecation_warning(\n",
      "\u001b[0m2024-12-30 21:14:11 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Restored component 'CountVectorsFeaturizer' from cache.\n",
      "2024-12-30 21:14:11 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Restored component 'CountVectorsFeaturizer' from cache.\n",
      "2024-12-30 21:14:12 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Restored component 'DIETClassifier' from cache.\n",
      "2024-12-30 21:14:12 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Restored component 'EntitySynonymMapper' from cache.\n",
      "2024-12-30 21:14:12 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Restored component 'LexicalSyntacticFeaturizer' from cache.\n",
      "2024-12-30 21:14:12 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Restored component 'RegexFeaturizer' from cache.\n",
      "2024-12-30 21:14:12 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Restored component 'ResponseSelector' from cache.\n",
      "2024-12-30 21:14:12 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Restored component 'RulePolicy' from cache.\n",
      "2024-12-30 21:14:12 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Restored component 'TEDPolicy' from cache.\n",
      "2024-12-30 21:14:12 \u001b[1;30mINFO    \u001b[0m \u001b[34mrasa.engine.training.hooks\u001b[0m  - Restored component 'UnexpecTEDIntentPolicy' from cache.\n",
      "\u001b[92mYour Rasa model is trained and saved at 'models/20241230-211410-synchronic-diff.tar.gz'.\u001b[0m\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "### Running Servers"
   ],
   "metadata": {
    "id": "sQy3kIUMONDl"
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "The **Rasa Server** is the core component that handles user interactions. It processes incoming messages, interprets user intents, manages conversations, and generates appropriate responses based on the trained model and defined dialogue flows."
   ],
   "metadata": {
    "id": "zVnktY3tODA6"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "import threading\n",
    "import os\n",
    "\n",
    "\n",
    "def run_rasa_server():\n",
    "    os.system(\"rasa run\")\n",
    "\n",
    "\n",
    "threading.Thread(target=run_rasa_server).start()\n",
    "# Wait for the Rasa server to start\n",
    "time.sleep(35)"
   ],
   "metadata": {
    "id": "NZq0FZgO_yCL"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": [
    "The **Action Server** is a separate service that executes Custom Actions defined in your project (actions.py). These actions perform specialized tasks such as querying databases, processing data, or integrating with external APIs."
   ],
   "metadata": {
    "id": "FYV_JvcsOIuM"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "import threading\n",
    "import os\n",
    "\n",
    "\n",
    "def run_action_server():\n",
    "    os.system(\"rasa run actions\")\n",
    "\n",
    "\n",
    "threading.Thread(target=run_action_server).start()\n",
    "# Wait for the Rasa server to start\n",
    "time.sleep(35)"
   ],
   "metadata": {
    "id": "op2C5AsO_yFA"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": [
    "### Let's test out chatbot"
   ],
   "metadata": {
    "id": "X_v2YzDvOUff"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "# Function to send messages to the Rasa server\n",
    "def send_message(message):\n",
    "    url = \"http://localhost:5005/webhooks/rest/webhook\"\n",
    "    payload = {\"sender\": \"test_user\", \"message\": message}\n",
    "    headers = {\"Content-Type\": \"application/json\"}\n",
    "    try:\n",
    "        response = requests.post(url, data=json.dumps(payload), headers=headers)\n",
    "        return response.json()\n",
    "    except requests.exceptions.ConnectionError:\n",
    "        return {\"error\": \"Could not connect to Rasa server.\"}\n",
    "\n",
    "\n",
    "# Example interactions\n",
    "print(\"User: Hi\")\n",
    "assistant_response = send_message(\"Hi\")\n",
    "if assistant_response:\n",
    "    for resp in assistant_response:\n",
    "        if isinstance(resp, dict) and \"text\" in resp:\n",
    "            print(\"Assistant:\", resp[\"text\"])\n",
    "        else:\n",
    "            print(\"Assistant:\", resp)\n",
    "\n",
    "print(\"\\nUser: How do I reset my password? Explain in french\")\n",
    "assistant_response = send_message(\"How do I delete my account? Explain in french\")\n",
    "if assistant_response:\n",
    "    print(\"Assistant:\", end=\" \")\n",
    "    for resp in assistant_response:\n",
    "        if isinstance(resp, dict) and \"text\" in resp:\n",
    "            print(resp[\"text\"])\n",
    "        else:\n",
    "            print(resp)\n",
    "\n",
    "print(\"\\nUser: Standard shipping details. Hinglish mein btao\")\n",
    "assistant_response = send_message(\"Standard shipping details. Hinglish mein btao\")\n",
    "if assistant_response:\n",
    "    print(\"Assistant:\", end=\" \")\n",
    "    for resp in assistant_response:\n",
    "        if isinstance(resp, dict) and \"text\" in resp:\n",
    "            print(resp[\"text\"])\n",
    "        else:\n",
    "            print(resp)\n",
    "\n",
    "print(\"\\nUser: What is the weather today?\")\n",
    "assistant_response = send_message(\"What is the weather today?\")\n",
    "if assistant_response:\n",
    "    print(\"Assistant:\", end=\" \")\n",
    "    for resp in assistant_response:\n",
    "        if isinstance(resp, dict) and \"text\" in resp:\n",
    "            print(resp[\"text\"])\n",
    "        else:\n",
    "            print(resp)\n",
    "\n",
    "print(\"\\nUser: Bye\")\n",
    "assistant_response = send_message(\"Bye\")\n",
    "if assistant_response:\n",
    "    print(\"Assistant:\", end=\" \")\n",
    "    for resp in assistant_response:\n",
    "        if isinstance(resp, dict) and \"text\" in resp:\n",
    "            print(resp[\"text\"])\n",
    "        else:\n",
    "            print(resp)"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "GxKWasrDOT5D",
    "outputId": "9cb6f54e-5b3c-4bdb-fc3e-236937db13d5"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "User: Hi\n",
      "Assistant: Hello! How can I assist you today?\n",
      "\n",
      "User: How do I reset my password? Explain in french\n",
      "Assistant: Bonjour,\n",
      "Pour supprimer votre compte, veuillez contacter notre équipe d'assistance à l'adresse suivante : support@yourcompany.com. Assurez-vous d'indiquer dans l'objet de votre message \"Demande de suppression de compte\". Nous vous assisterons rapidement dans cette démarche.\n",
      "Si vous avez d'autres questions ou si vous avez besoin d'une aide supplémentaire, n'hésitez pas à nous le faire savoir.\n",
      "Cordialement,  \n",
      "L'équipe de support\n",
      "\n",
      "User: Standard shipping details. Hinglish mein btao\n",
      "Assistant: Aapka swaagat hai! Humein khushi hai ki aapne humse sampark kiya. \n",
      "Hamara standard shipping policy yeh hai ki agar aapka order $75 se zyada hai, toh aapko standard shipping par koi bhi charges nahi dene honge, yani shipping free hai. Lekin agar aapka order is amount se kam hai, toh aapko standard shipping fees deni padegi.\n",
      "Agar aapko aur koi sawaal hai ya madad chahiye, toh zaroor puchhiye!\n",
      "\n",
      "User: What is the weather today?\n",
      "Assistant: I'm sorry, I don't have an answer to that question.\n",
      "\n",
      "User: Bye\n",
      "Assistant: Goodbye! Have a great day!\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "### For debugging purposes"
   ],
   "metadata": {
    "id": "eAli94VAZXpV"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "!lsof -i"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "fqBXLg3BU0Pn",
    "outputId": "bfa0a50f-f665-414d-ad69-95244dc81850"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME\n",
      "node         6 root   21u  IPv6  19762      0t0  TCP *:8080 (LISTEN)\n",
      "node         6 root   27u  IPv4  21268      0t0  TCP 11757d4d820b:56654->11757d4d820b:6000 (ESTABLISHED)\n",
      "node         6 root   28u  IPv6  68552      0t0  TCP 11757d4d820b:8080->172.28.0.1:49662 (ESTABLISHED)\n",
      "node         6 root   29u  IPv6 142453      0t0  TCP 11757d4d820b:8080->172.28.0.1:43440 (ESTABLISHED)\n",
      "node         6 root   31u  IPv6  72491      0t0  TCP 11757d4d820b:8080->172.28.0.1:46072 (ESTABLISHED)\n",
      "node         6 root   34u  IPv6 143833      0t0  TCP 11757d4d820b:8080->172.28.0.1:43708 (ESTABLISHED)\n",
      "kernel_ma   26 root    3u  IPv4  19626      0t0  TCP 11757d4d820b:6000 (LISTEN)\n",
      "kernel_ma   26 root    6u  IPv4  21269      0t0  TCP 11757d4d820b:6000->11757d4d820b:56654 (ESTABLISHED)\n",
      "kernel_ma   26 root    7u  IPv4  20263      0t0  TCP 11757d4d820b:41868->11757d4d820b:9000 (ESTABLISHED)\n",
      "kernel_ma   26 root    8u  IPv4  21271      0t0  TCP 11757d4d820b:46128->11757d4d820b:9000 (ESTABLISHED)\n",
      "kernel_ma   26 root    9u  IPv4  21384      0t0  TCP 11757d4d820b:6000->172.28.0.1:58950 (ESTABLISHED)\n",
      "colab-fil   73 root    3u  IPv4  19800      0t0  TCP localhost:3453 (LISTEN)\n",
      "jupyter-n   90 root    7u  IPv4  20838      0t0  TCP 11757d4d820b:9000 (LISTEN)\n",
      "jupyter-n   90 root    8u  IPv4  20866      0t0  TCP 11757d4d820b:9000->11757d4d820b:41868 (ESTABLISHED)\n",
      "jupyter-n   90 root   18u  IPv4  21272      0t0  TCP 11757d4d820b:9000->11757d4d820b:46128 (ESTABLISHED)\n",
      "dap_multi   91 root    9u  IPv4  68985      0t0  TCP localhost:58084->localhost:38429 (ESTABLISHED)\n",
      "python3   1728 root   21u  IPv4  68701      0t0  TCP localhost:37327 (LISTEN)\n",
      "python3   1728 root   34u  IPv4  68878      0t0  TCP localhost:44974->localhost:41041 (ESTABLISHED)\n",
      "python3   1728 root   51u  IPv4 106476      0t0  TCP 11757d4d820b:42606->server-3-171-171-65.atl59.r.cloudfront.net:443 (CLOSE_WAIT)\n",
      "python3   1728 root   52u  IPv4 106845      0t0  TCP 11757d4d820b:46718->server-3-171-171-65.atl59.r.cloudfront.net:443 (CLOSE_WAIT)\n",
      "python3   1728 root   54u  IPv4 108879      0t0  TCP 11757d4d820b:34598->server-54-230-253-56.atl56.r.cloudfront.net:443 (CLOSE_WAIT)\n",
      "python3   1753 root    3u  IPv4  68533      0t0  TCP localhost:38429 (LISTEN)\n",
      "python3   1753 root    4u  IPv4  68875      0t0  TCP localhost:38429->localhost:58084 (ESTABLISHED)\n",
      "python3   1753 root    5u  IPv4  68534      0t0  TCP localhost:41041 (LISTEN)\n",
      "python3   1753 root    6u  IPv4  68876      0t0  TCP localhost:41041->localhost:44974 (ESTABLISHED)\n",
      "rasa      6257 root   14u  IPv4 140831      0t0  TCP *:5005 (LISTEN)\n",
      "rasa      6434 root   29u  IPv4 139178      0t0  TCP *:5055 (LISTEN)\n",
      "rasa      6434 root   32u  IPv4 140924      0t0  TCP 11757d4d820b:51776->server-3-171-171-6.atl59.r.cloudfront.net:443 (ESTABLISHED)\n"
     ]
    }
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "!kill -9 7966"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "RgVuA2MiVQfR",
    "outputId": "4c515fc8-b2eb-4566-d681-b48c06e06d0b"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "/bin/bash: line 1: kill: (7966) - No such process\n"
     ]
    }
   ]
  },
  {
   "cell_type": "code",
   "source": [],
   "metadata": {
    "id": "Wrg5YFSlWFd-"
   },
   "execution_count": null,
   "outputs": []
  }
 ]
}